﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace AuthenticationServer.Data
{
    //public class DesignTimeDbContextFactory
    //{
    //}

    public class DesignTimeDbContextFactory<TDbContext, TStoreOptions> : IDesignTimeDbContextFactory<TDbContext>
        where TDbContext : DbContext
        where TStoreOptions : class, new() 
    {
        public TDbContext CreateDbContext(string[] args)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            var builder = new DbContextOptionsBuilder<TDbContext>();

            var connectionString = configuration.GetConnectionString("DefaultConnection");

            builder.UseSqlServer(connectionString);

            var dbContext = (TDbContext)Activator.CreateInstance(
                typeof(TDbContext),
                builder.Options,
                new TStoreOptions());

            return dbContext;
        }
    }

}
